﻿namespace com.crownpack
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using com.crownpack.Extensions.StringExtensions;

    using Microsoft.SharePoint;

    public class CustomerFieldValue
    {
        #region Fields

        string _LocationName;
        int? _LocationNo;
        int? _ParentCustomerNo;
        Customer _UnderlyingObject;

        #endregion Fields

        #region Constructors

        public CustomerFieldValue( )
        {
        }

        public CustomerFieldValue( object value, string currentWebUrl )
            : this(( SPFieldMultiColumnValue ) value, currentWebUrl)
        {
        }

        public CustomerFieldValue( SPFieldMultiColumnValue value, string currentWebUrl )
            : this(value)
        {
            CurrentWebUrl = currentWebUrl;
        }

        public CustomerFieldValue( object value )
            : this(( SPFieldMultiColumnValue ) value)
        {
        }

        public CustomerFieldValue( SPFieldMultiColumnValue value )
        {
            ParentCustomerNo = value[ 0 ].ToNullableInt32( );
            LocationNo = value[ 1 ].ToNullableInt32( );
            LocationName = value[ 2 ].HtmlDecode( );
        }

        #endregion Constructors

        #region Properties

        public string LocationName
        {
            get
            {
                return _LocationName.HtmlDecode( );
            }
            set
            {
                _LocationName = value.HtmlEncode( );
            }
        }

        public int? LocationNo
        {
            get
            {
                return _LocationNo;
            }
            set
            {
                _LocationNo = value;
            }
        }

        public int? ParentCustomerNo
        {
            get
            {
                return _ParentCustomerNo;
            }
            set
            {
                _ParentCustomerNo = value;
            }
        }

        public Customer UnderlyingObject
        {
            get
            {
                if ( _UnderlyingObject == null )
                {
                    if ( ParentCustomerNo.HasValue && LocationNo.HasValue )
                    {
                        LOBDao dao = new LOBDao( CurrentWebUrl );
                        _UnderlyingObject = dao.GetByLocationNo( ParentCustomerNo.Value, LocationNo.Value );
                    }
                }
                return _UnderlyingObject;
            }
        }

        string CurrentWebUrl
        {
            get;
            set;
        }

        #endregion Properties

        #region Methods

        public string GetHtmlFormat( )
        {
            return toString( ).Replace( "\t", "<br />" );
        }

        public override string ToString( )
        {
            return toString( ).Replace( "\t", ", " );
        }

        string toString( )
        {
            StringBuilder sb = new StringBuilder( );
            if ( !string.IsNullOrEmpty( LocationName ) )
                sb.AppendFormat( "{0}\t", LocationName );

            string s = sb.ToString( ).Trim( ).Trim( '\t' );
            sb = null;
            return s;
        }

        #endregion Methods
    }
}